package service;

import common.Pagination;
import dao.CroBuycarsDao;
import entity.CroBuycars;
import entity.CroPurchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chris on 17-5-11.
 */
@Service
public class CroBuycarsService {

    @Autowired
    private CroBuycarsDao croBuycarsDao;

    /**
     * 根据订单查找购物车
     * @param purchase 订单
     * @return
     */
    @Transactional(readOnly = true)
    public List<CroBuycars> listByPurchase(CroPurchase purchase) {
        Map<String,Object> map=new HashMap<>();
        map.put("croPurchase",purchase);
        map.put("del_flag","0");
        return croBuycarsDao.findByProperty(map);
    }

    /**
     * 根据订单Id查找购物车
     * @param Id 订单Id
     * @return
     */
    @Transactional(readOnly = true)
    public List<CroBuycars> listByPurchaseId(String Id) {
        Map<String,Object> map=new HashMap<>();
        map.put("purchaseid",Id);
        map.put("del_flag","0");
        return croBuycarsDao.findByProperty(map);
    }

    /**
     * 通过id查找购物车
     * @param id
     * @return
     */
    @Transactional(readOnly = true)
    public CroBuycars findById(String id){
        return croBuycarsDao.getById(id);
    }


    @Transactional(readOnly = true)
    public List<CroBuycars> list() {
        String hql = "from CroBuycars";
        return croBuycarsDao.findList(hql);
    }

    /*
    * 分页查询出购物车内未下单的商品
    * */
    @Transactional(readOnly = true)
    public Pagination<CroBuycars> FindByPurchase(CroPurchase croPurchase,int index,int size){
        Map<String,Object> map=new HashMap<>();
        map.put("purchaseid",croPurchase);
        map.put("del_flag","0");
        Pagination<CroBuycars> page=croBuycarsDao.findPaginationByProperty(map,index,size);
        return page;
    }

    /*
    * 根据id删除购物车内相应商品
    * */
    @Transactional(readOnly = false)
    public boolean DeleteBuyCarById(String id,String del_flag){
        try {
            CroBuycars croBuycars=croBuycarsDao.getById(id);
            croBuycars.setDel_flag(del_flag);
            croBuycarsDao.update(croBuycars);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 后台更新购物车相应商品信息
    * */
    @Transactional(readOnly = false)
    public boolean UpdateBuyCarById(String id,String actprice,String numbers){
        try {
            CroBuycars croBuycars=croBuycarsDao.getById(id);
            double price=Double.parseDouble(actprice);
            int num=Integer.parseInt(numbers);
            croBuycars.setActualprice(price);
            croBuycars.setNumbers(num);
            croBuycars.setSumprice(price*num);
            croBuycarsDao.update(croBuycars);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
  * 前台用户更新购物车相应商品信息
  * */
    @Transactional(readOnly = false)
    public boolean UpdateBuyCarById(String id,String numbers){
        try {
            CroBuycars croBuycars=croBuycarsDao.getById(id);
            int num=Integer.parseInt(numbers);
            croBuycars.setNumbers(num);
            croBuycars.setSumprice(croBuycars.getActualprice()*num);
            croBuycarsDao.update(croBuycars);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 添加或更新购物车
     * @param croBuycars 物品
     * @return boolean
     */
    @Transactional(readOnly = false)
    public boolean saveOrUpdateBuyCars(CroBuycars croBuycars){
        try{
            croBuycarsDao.saveOrUpdate(croBuycars);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
